<template>
    <div class="coding-overview-container">
        <global-search @submitSearch="submitSearch" class="global-tool-search"/>
        <overview-card :cardName="cardName.cardList" :cardData="cardData" listUrlName="CodingListIndex"/>
        <div class="flex-box">
            <overview-echart titleText="coding" :initCardParams="initCardParams" :cardName="cardName.echart" :echartData="echartData"/>
            <div class="table-top10">
                <div class="head">
                    <h3>编码缺陷提示TOP10统计</h3>
                    <div class="radio-box">
                        <el-radio-group v-model="defType" @change="changeList">
                            <el-radio label="0">强制缺陷</el-radio>
                            <el-radio label="1">疑似缺陷</el-radio>
                            <el-radio label="2">疑似缺陷(忽略项)</el-radio>
                        </el-radio-group>
                    </div>
                </div>
                <el-table
                    ref="tableData"
                    :data="tableData"
                    tooltip-effect="light"
                    border
                    class="data-table">
                    <el-table-column
                        label="排名"
                        type="index"
                        align="center"
                        width="60">
                    </el-table-column>
                    <el-table-column
                        prop="msg"
                        label="缺陷提示"
                        show-overflow-tooltip>
                    </el-table-column>
                    <el-table-column
                        prop="num"
                        label="病案数(例)"
                        align="center"
                        width="120"
                        show-overflow-tooltip>
                        <template slot-scope="scope">
                            <span v-if="scope.row.num > 0" class="to-child-page" @click="toMRList(scope.row)">{{scope.row.num}}</span>
                            <span v-else>{{scope.row.num}}</span>
                        </template>
                    </el-table-column>
                </el-table>
            </div>
        </div>
    </div>
</template>

<script>
    import {getCodingDefectAll, getCodingDefectTrend, getCodingDefectTop10} from '@/api/quality-control/coding-defect'
    import OverviewEchart from '@/components/OverviewEchart'
    import OverviewCard from '@/components/OverviewCard'
    import {getZeroTwoNum} from '@/utils/filters'

    export default {
        name: 'index',
        data () {
            return {
                // 配置echart卡片参数
                initCardParams: {
                    width: 'calc(58% - 10px)',
                    height: '530px',
                    type: 'line'
                },
                // 卡片名称
                cardName: {
                    cardList: '编码缺陷概览',
                    echart: '编码缺陷趋势分析'
                },
                // 缺陷选择
                defType: '0',
                // 列表数据
                tableData: [],
                // 三类列表数据
                allTableData: [],
                // 卡片数据
                cardData: [],
                // 图表数据
                echartData: [],
                // 使用储存的数据，不去请求
                useStoreAll: false,
                useStoreTrend: false,
                useStoreTop10: false
            }
        },
        created () {
            // tags判断入口是否从选项卡进入
            this.useStoreAll = this.$route.query.tags === 'tags'
            this.useStoreTrend = this.$route.query.tags === 'tags'
            this.useStoreTop10 = this.$route.query.tags === 'tags'
        },
        methods: {
            getZeroTwoNum,
            /**
             * 提交查询
             * @param form 筛选条件
             */
            submitSearch (form) {
                let params = {
                    start: form.start,
                    end: form.end,
                    orgId: form.orgId,
                    department: form.department,
                    departmentName: form.departmentName,
                    coder: form.coder,
                    isLast: form.isLast
                }
                this.setGetStore(this.$route.name, params)
                this.getDataAll(params)
                this.getDataTrend(params)
                this.getDataTop10(params)
            },
            /**
             * 获取缺陷总览
             * @param data 请求数据
             */
            getDataAll (data) {
                if (this.getSession(this.$route.name, 'getCodingDefectAll') && this.useStoreAll) {
                    this.useStoreAll = false
                    let res = this.getSession(this.$route.name, 'getCodingDefectAll')
                    if (res.code.toString() === '200') {
                        // 顶部卡片数据
                        this.cardData = [
                            {
                                name: '质控总病案数',
                                unit: '例',
                                tipsUnit: '%',
                                number: res.data.total,
                                intuitive: getZeroTwoNum(res.data.totalCom * 100),
                                urlName: 'CodingListIndex',
                                defectSate: 0
                            }, {
                                name: '编码缺陷病案数',
                                unit: '例',
                                tipsUnit: '%',
                                number: res.data.defect,
                                intuitive: getZeroTwoNum(res.data.defectCom * 100),
                                urlName: 'CodingListIndex',
                                defectSate: 5
                            }, {
                                name: '编码缺陷率',
                                unit: '%',
                                tipsUnit: '%',
                                number: getZeroTwoNum(res.data.defectRate * 100),
                                intuitive: getZeroTwoNum(res.data.defectRateCom * 100),
                                urlName: 'CodingListIndex',
                                defectSate: 5
                            }, {
                                name: '诊断编码缺陷病案数',
                                unit: '例',
                                tipsUnit: '%',
                                number: res.data.diagnosis,
                                intuitive: getZeroTwoNum(res.data.diagnosisCom * 100),
                                urlName: 'CodingListIndex',
                                defectSate: 7
                            }, {
                                name: '手术操作编码缺陷病案数',
                                unit: '例',
                                tipsUnit: '%',
                                number: res.data.surgery,
                                intuitive: getZeroTwoNum(res.data.surgeryCom * 100),
                                urlName: 'CodingListIndex',
                                defectSate: 9
                            }, {
                                name: '主要诊断缺陷病案数',
                                unit: '例',
                                tipsUnit: '%',
                                number: res.data.mainDiagnosis,
                                intuitive: getZeroTwoNum(res.data.mainDiagnosisCom * 100),
                                urlName: 'CodingListIndex',
                                defectSate: 8
                            }, {
                                name: '主要手术缺陷病案数',
                                unit: '例',
                                tipsUnit: '%',
                                number: res.data.mainSurgery,
                                intuitive: getZeroTwoNum(res.data.mainSurgeryCom * 100),
                                urlName: 'CodingListIndex',
                                defectSate: 10
                            }, {
                                name: '门(急)诊诊断编码缺陷病案数',
                                unit: '例',
                                tipsUnit: '%',
                                number: res.data.outpatientEmergency,
                                intuitive: getZeroTwoNum(res.data.outpatientEmergencyCom * 100),
                                urlName: 'CodingListIndex',
                                defectSate: 11
                            }, {
                                name: '损伤中毒外因编码缺陷病案数',
                                unit: '例',
                                tipsUnit: '%',
                                number: res.data.externalCauses,
                                intuitive: getZeroTwoNum(res.data.externalCausesCom * 100),
                                urlName: 'CodingListIndex',
                                defectSate: 12
                            }, {
                                name: '病理诊断编码缺陷病案数',
                                unit: '例',
                                tipsUnit: '%',
                                number: res.data.pathology,
                                intuitive: getZeroTwoNum(res.data.pathologyCom * 100),
                                urlName: 'CodingListIndex',
                                defectSate: 13
                            }
                        ]
                    }
                } else {
                    getCodingDefectAll(data).then(res => {
                        this.useStoreAll = false
                        this.setSession(this.$route.name, {getCodingDefectAll: res})
                        if (res.code.toString() === '200') {
                            // 顶部卡片数据
                            this.cardData = [
                                {
                                    name: '质控总病案数',
                                    unit: '例',
                                    tipsUnit: '%',
                                    number: res.data.total,
                                    intuitive: getZeroTwoNum(res.data.totalCom * 100),
                                    urlName: 'CodingListIndex',
                                    defectSate: 0
                                }, {
                                    name: '编码缺陷病案数',
                                    unit: '例',
                                    tipsUnit: '%',
                                    number: res.data.defect,
                                    intuitive: getZeroTwoNum(res.data.defectCom * 100),
                                    urlName: 'CodingListIndex',
                                    defectSate: 5
                                }, {
                                    name: '编码缺陷率',
                                    unit: '%',
                                    tipsUnit: '%',
                                    number: getZeroTwoNum(res.data.defectRate * 100),
                                    intuitive: getZeroTwoNum(res.data.defectRateCom * 100),
                                    urlName: 'CodingListIndex',
                                    defectSate: 5
                                }, {
                                    name: '诊断编码缺陷病案数',
                                    unit: '例',
                                    tipsUnit: '%',
                                    number: res.data.diagnosis,
                                    intuitive: getZeroTwoNum(res.data.diagnosisCom * 100),
                                    urlName: 'CodingListIndex',
                                    defectSate: 7
                                }, {
                                    name: '手术操作编码缺陷病案数',
                                    unit: '例',
                                    tipsUnit: '%',
                                    number: res.data.surgery,
                                    intuitive: getZeroTwoNum(res.data.surgeryCom * 100),
                                    urlName: 'CodingListIndex',
                                    defectSate: 9
                                }, {
                                    name: '主要诊断缺陷病案数',
                                    unit: '例',
                                    tipsUnit: '%',
                                    number: res.data.mainDiagnosis,
                                    intuitive: getZeroTwoNum(res.data.mainDiagnosisCom * 100),
                                    urlName: 'CodingListIndex',
                                    defectSate: 8
                                }, {
                                    name: '主要手术缺陷病案数',
                                    unit: '例',
                                    tipsUnit: '%',
                                    number: res.data.mainSurgery,
                                    intuitive: getZeroTwoNum(res.data.mainSurgeryCom * 100),
                                    urlName: 'CodingListIndex',
                                    defectSate: 10
                                }, {
                                    name: '门(急)诊诊断编码缺陷病案数',
                                    unit: '例',
                                    tipsUnit: '%',
                                    number: res.data.outpatientEmergency,
                                    intuitive: getZeroTwoNum(res.data.outpatientEmergencyCom * 100),
                                    urlName: 'CodingListIndex',
                                    defectSate: 11
                                }, {
                                    name: '损伤中毒外因编码缺陷病案数',
                                    unit: '例',
                                    tipsUnit: '%',
                                    number: res.data.externalCauses,
                                    intuitive: getZeroTwoNum(res.data.externalCausesCom * 100),
                                    urlName: 'CodingListIndex',
                                    defectSate: 12
                                }, {
                                    name: '病理诊断编码缺陷病案数',
                                    unit: '例',
                                    tipsUnit: '%',
                                    number: res.data.pathology,
                                    intuitive: getZeroTwoNum(res.data.pathologyCom * 100),
                                    urlName: 'CodingListIndex',
                                    defectSate: 13
                                }
                            ]
                        } else {
                            this.$message.error(res.message)
                        }
                    })
                }
            },
            /**
             * 获取缺陷走势分析
             * @param data 请求数据
             */
            getDataTrend (data) {
                if (this.getSession(this.$route.name, 'getCodingDefectTrend') && this.useStoreTrend) {
                    this.useStoreTrend = false
                    let res = this.getSession(this.$route.name, 'getCodingDefectTrend')
                    if (res.code.toString() === '200') {
                        this.echartData = [{
                            name: '编码缺陷项',
                            data: res.data.num,
                            xIndex: res.data.xIndex
                        }]
                    }
                } else {
                    getCodingDefectTrend(data).then(res => {
                        this.useStoreTrend = false
                        this.setSession(this.$route.name, {getCodingDefectTrend: res})
                        if (res.code.toString() === '200') {
                            this.echartData = [{
                                name: '编码缺陷项',
                                data: res.data.num,
                                xIndex: res.data.xIndex
                            }]
                        } else {
                            this.$message.error(res.message)
                        }
                    })
                }
            },
            /**
             * 获取缺陷top10
             * @param data 请求数据
             */
            getDataTop10 (data) {
                if (this.getSession(this.$route.name, 'getCodingDefectTop10') && this.useStoreTop10) {
                    this.useStoreTop10 = false
                    let res = this.getSession(this.$route.name, 'getCodingDefectTop10')
                    if (res.code.toString() === '200') {
                        this.allTableData = res.data
                        this.changeList()
                    }
                } else {
                    getCodingDefectTop10(data).then(res => {
                        this.useStoreTop10 = false
                        this.setSession(this.$route.name, {getCodingDefectTop10: res})
                        if (res.code.toString() === '200') {
                            this.allTableData = res.data
                            this.changeList()
                        } else {
                            this.$message.error(res.message)
                        }
                    })
                }
            },
            /**
             * 修改类型单选框
             */
            changeList () {
                switch (this.defType) {
                case '0':
                    this.tableData = this.allTableData.error
                    break
                case '1':
                    this.tableData = this.allTableData.waring
                    break
                case '2':
                    this.tableData = this.allTableData.ignore
                    break
                }
            },
            /**
             * 前往病案明细
             * @param row 行数据
             */
            toMRList (row) {
                let query = JSON.parse(JSON.stringify(this.$store.state.search.searchForm[this.$route.name]))
                let params = {
                    ...query,
                    errReason: row.msg,
                    defectSate: 5,
                    pageIndex: 1
                }
                this.$store.dispatch('search/coverSearchFormItem', {
                    pageName: 'CodingListIndex',
                    query: params
                })
                this.$router.push({name: 'CodingListIndex'})
            }
        },
        components: {
            OverviewEchart,
            OverviewCard
        }
    }
</script>

<style lang="scss" scoped>
    .coding-overview-container {
        .flex-box {
            display: flex;
            justify-content: space-between;
            align-items: flex-start;
            margin-top: 10px;
            .table-top10 {
                width: 42%;
                height: 530px;
                background: #fff;
                padding: 0 10px;
                box-sizing: border-box;
                .head {
                    display: flex;
                    justify-content: space-between;
                    align-items: center;
                    h3 {
                        font-size: 16px;
                        color: #000;
                        font-weight: 600;
                        line-height: 48px;
                    }
                    .el-radio {
                        margin-right: 10px;
                        &:last-child {
                            margin-right: 0;
                        }
                    }
                }
                /deep/.el-table__row td {
                    padding: 0;
                    div {
                        height: 40px;
                        line-height: 40px;
                    }
                }
            }
        }
    }
</style>
